package com.frkj.yuan.api.presenter;

import android.util.Log;

import com.frkj.yuan.api.bean.UploadNichengMsgBean;
import com.frkj.yuan.api.category.OrderCommentCategory;
import com.frkj.yuan.base.BaseBean;
import com.frkj.yuan.utils.RetrofitUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.luck.picture.lib.entity.LocalMedia;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 信息说明:
 * company: 兰州丰荣信息科技有限公司
 * 创建时间: on 2022/4/25 16 ：01
 * 项目名称：yuanban
 */
public class OrderCommentPresenter implements OrderCommentCategory.Presenter {
    OrderCommentCategory.View view;
    private String image;

    public OrderCommentPresenter(OrderCommentCategory.View view) {
        this.view = view;
    }

    public void getOrderComment(String mToken, long userId, long orderId, String orderNo, String content, float score, String tags, int isAnonymous, List<LocalMedia> imgStrs) {
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型
            for (int i = 0; i < imgStrs.size(); i++) {
                image = imgStrs.get(i).getCompressPath();
                File  file = new File(image);
                RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                builder.addFormDataPart("imgs", file.getName(), requestFile);
            }
        builder.addFormDataPart("content", content);
        builder.addFormDataPart("score", String.valueOf(score));
        builder.addFormDataPart("tags", tags);
        builder.addFormDataPart("isAnonymous", String.valueOf(isAnonymous));
        builder.addFormDataPart("orderNo", orderNo);
        List<MultipartBody.Part> parts = builder.build().parts();
        RetrofitUtil.getInstance().getmService().upload_file_more(mToken,orderId,String.valueOf(userId),parts).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String result = null;
                try {
                    result = response.body().string();
                    Gson gson = new GsonBuilder().serializeNulls().create();
                    BaseBean userInfor = gson.fromJson(result, BaseBean.class);
                    view.getOrderComment(userInfor);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                view.OnShowErrorMsg(t.getMessage());
            }
        });
    }
}
